import api from "../../Sendingmethod/Sendingmethod"
// pages/equipment/equipment.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //底部导航栏 index
        tabarindex: "pages/equipment/equipment",
        // 设备状态
        emtype: [],
        emtype_index: "",
        //控制弹窗
        ctrlprop_index: false,
        dtype: {},
        emtlist: [],
        pageindex: 1,
        lodingblen: false,
        triggered: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options, '首页传入数据');
        //获取状态栏高度
        const statusBarHeight = wx.getWindowInfo().statusBarHeight;
        //获取胶囊属性
        const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
        //计算导航栏属性
        const navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2
        //赋值给 高度 变量
        this.setData({
            statusBar_H: (statusBarHeight + navBarHeight) * 2,
            menu_Info: statusBarHeight * 2
        })
        console.log(statusBarHeight, navBarHeight, this.statusBar_H, '页面刷新');
        this.getdevicestatusList()
        this.getdevicetype()
    },


    /**
     * 选择
     */
    emtype_select(e) {
        console.log(e.target.dataset.index, '选择');
        if (this.data.emtype_index !== e.target.dataset.index) {
            this.setData({
                emtype_index: e.target.dataset.index,
                pageindex: 1
            })
            this.getdevicelist(1)
        }

    },
    /**
     * 
     * 字符串截取
     */
    extractNumberIdFromUrl(url) {
        // 使用正则表达式 /\d+/g 来匹配URL中的数字ID
        const matches = url.match(/id=(\d+)/);
        if (matches && matches.length > 1) {
            return matches[1]; // 返回第一个匹配项，即 id 后面的数字
        }
        return null; // 如果没有找到匹配项，则返回 null
    },

    /**
     *扫一扫
     * 
     */
    opensaoma() {
        let _that = this
        // wx.scanCode({
        //     success(res) {
        //         console.log(res.result);
        //         console.log(_that.extractNumberIdFromUrl(res.result), '设备id截取');
        //         let id = _that.extractNumberIdFromUrl(res.result)
        //         console.log(id, '设备id');
        //         wx.navigateTo({
        //             url: `/pages/Device_details/Device_details?did=${id}`,
        //         })
        //     }
        // })
        wx.navigateTo({
          url: '/pages/Camera/Camera',
        })

    },
    /**
     * 控制弹窗
     */
    ctrlprop() {
        console.log(this.data.ctrlprop_index,'当前111111111111111111111111111111111');
        let blen=this.data.ctrlprop_index
        this.setData({
            ctrlprop_index: !blen
        })
    },
    /**
     * 
     */
    goto1(e) {
        console.log(e);
        console.log(e.currentTarget.dataset.emid);
        wx.navigateTo({
            url: `/pages/Device_details/Device_details?emid=${e.currentTarget.dataset.emid}`,
        })
    },
    goto2(e) {
        console.log(e.currentTarget.dataset.emid);
        if (e.target.dataset.index == 1) {
            wx.navigateTo({
                url: `/pages/Add_device/Add_device?emid=${e.currentTarget.dataset.emid}`,
            })
        } else if (e.target.dataset.index == 2) {
            wx.navigateTo({
                url: '/pages/Device_details/Device_details',
            })
        }
    },
    /**
     * xialshuax 下拉刷新
     */
    xialshuax() {
        wx.showLoading({
            mask: true
        })
        console.log('下拉刷新触发');
        this.getdevicelist(1)
        setTimeout(() => {
            wx.hideLoading()
            this.setData({
                triggered: false
            })
        }, 1000)
    },
    /**
     * 获取设备类型
     */
    async getdevicetype() {
        try {
            let res = await api.devicetype()
            if (res.code == 0) {
                console.log(res, '设备类型');
                this.setData({
                    dtype: res.data
                })
            }
        } catch (error) {

        }
    },
    /**
     * 获取设备状态
     */
    async getdevicestatusList() {
        try {
            let res = await api.devicestatusList()
            if (res.code == 0) {
                console.log(res, '设备状态');
                this.setData({
                    emtype: [
                        ...[{
                            id: "",
                            name: '全部'
                        }],
                        ...res.data
                    ]
                })
                console.log(this.data.emtype,'设备状态');
            }
        } catch (error) {

        }
    },
    /**
     * 获取设备列表
     */
    async getdevicelist(type) {
        console.log(this.data.emtype_index);
        try {
            let res = await api.devicelist({
                status: this.data.emtype_index,
                page: this.data.pageindex
            })
            if (res.code == 0) {
                console.log(res, type, '设备列表');
                let list = this.data.emtlist
                if (type == 1) {
                    console.log('走覆盖');
                    this.setData({
                        emtlist: res.data.list,
                        lodingblen: false
                    })
                } else {
                    console.log('走合并');
                    this.setData({
                        emtlist: [...list, ...res.data.list],
                        lodingblen: false
                    })
                }

                console.log(this.data.emtlist, '新设备列表');
            } else {
                this.setData({

                    lodingblen: false
                })
            }


        } catch (error) {
            this.setData({
                lodingblen: false
            })
        }
    },
    /**
     * 触底刷新
     */
    Bottomingout() {
        console.log('地步触发');
        this.setData({
            lodingblen: true
        })
        let list = this.data.emtlist
        setTimeout(() => {
            this.setData({
                emtlist: [...list, ...[1, 1, 1, 1, 1, 11, 1, 1]],
                lodingblen: false
            })
        }, 1000)


    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log('触发1111111111111111111');
      
        this.getdevicelist(1)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
       
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})